<?php

require ("sharedResources.php");

$sid = '';
try {
	// We need a SID (session ID) in order to operate
	$sid = login ( 'admin', 'admin' );
	echo 'sid: ' . $sid . PHP_EOL;
	
	$doc ['language'] = 'en';
	$doc ['fileName'] = 'testContent.txt';
	$doc ['folderId'] = DEFAULT_WORKSPACE; // create the document in DEFAULT_WORKSPACE

	// Requested parameters 
	// (although they are not evaluated during document creation)
	$doc ['creatorId'] = 0;
	$doc ['dateCategory'] = 0;
	$doc ['docType'] = 0;
	$doc ['exportStatus'] = 0;
	$doc ['fileSize'] = 0;
	$doc ['id'] = 0;
	$doc ['immutable'] = 0;
	$doc ['indexed'] = 0;
	$doc ['lengthCategory'] = 0;
	$doc ['publisherId'] = 0;
	$doc ['signed'] = 0;
	$doc ['size'] = 0;
	$doc ['status'] = 0;
	
	// This is new in 6.4	
	$doc ['published'] = 1;	
	
	$content = 'Test Content';
	
	$cdp = array ('sid' => $sid, 'document' => $doc, 'content' => $content );
	
	/*****************
	 * CREATE
	 * ***************/
	// Result is the document object of the new created document or a SoapFault exception
	$result = $documentClient->create ( $cdp );
	print_r ( $result );
	
	$documentID = $result->document->id;
	print_r ( "\nSdocument_id: " . $documentID . "\n\n" );
	
	/*****************
	 * READ
	 * ***************/
	$cdp = array ('sid' => $sid, 'docId' => $documentID );
	// 1) First invoke the method downloadDocumentInfo to get the filename
	$result = $documentClient->getDocument ( $cdp );
	print_r ( $result );
	
	// We save the document info in order to be able to update it subsequently
	$udoc = (array) $result->document; // convert a stdClass to array
	
	$myFileName = $result->document->fileName;
	print_r ( "\n\r" . $myFileName . "\n\r\n\r" );
	
	// 2) Then download the document (binary content)
	print_r ( "DOWNLOAD-DOCUMENT\r\n" );
	$cdp = array ('sid' => $sid, 'docId' => $documentID);
	
	$result = $documentClient->getContent ( $cdp );
	$content = $result->return;
	
	// Finally write the document on disk
	$myFile = "C:\\tmp\\" . $myFileName;
	$fh = fopen ( $myFile, 'w' ) or die ( "can't open file" );
	fwrite ( $fh, $content );
	fclose ( $fh );
	
	/*****************
	 * UPDATE
	 * ***************/
	print_r ( "UPDATE\r\n" );
	
	// Maybe we want to change some metadata informations
	$udoc ['title'] = 'PHP Changed Title';
	$udoc ['source'] = 'PHP-SOAP';
	$udoc ['sourceAuthor'] = 'myself';
	$udoc ['sourceDate'] = '2009-06-10';
	$udoc ['sourceType'] = 'sourceType';
	$udoc ['coverage'] = 'coverage';
	$udoc ['language'] = 'en';
	$udoc ['tags'] = array ('PHP', 'SOAP', 'webservice' );
	$udoc ['sourceId'] = 'sourceId';
	$udoc ['object'] = 'object';
	$udoc ['recipient'] = 'recipient';
	$udoc ['templateName'] = '';
	$udoc ['templateFields'] = null;
	
	$cdp = array ('sid' => $sid, 'document' => $udoc );
	
	$result = $documentClient->update ( $cdp );
	print_r ( $result );
	
	/*****************
	 * RE-READ
	 * ***************/
  print_r ( "RE-READ\r\n" ); 
	$cdp = array ('sid' => $sid, 'docId' => $documentID );
	// 1) First invoke the method downloadDocumentInfo to get the filename
	$result = $documentClient->getDocument ( $cdp );
	print_r ( $result );  
  
	/*****************
	 * DELETE
	 * ***************/
	print_r ( "DELETE\r\n" );
	$cdp = array ('sid' => $sid, 'docId' => $documentID );
	$result = $documentClient->delete ( $cdp );
	print_r ( $result );
	
	//unset ( $documentClient );
} catch ( SoapFault $e ) {
	print_r ( $e );
} 

// Always we need to logout in order to release connection slot
logout ( $sid );
?> 
	